<template>
  <div class="login">
    <div class="box">
      <el-form ref="form" :model="form" :rules="rules">
        <div class="img">
          <img src="@/assets/common/login-logo.png" alt="">
        </div>
        <el-form-item prop="mobile">
          <el-input v-model="form.mobile" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="submit">登陆</el-button>
        </el-form-item>
        <div style="width: 100%; text-align: center">
          <span style="color: #fff; cursor: pointer">还没有账号?立即注册</span>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              const reg =
                /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请填写正确的手机号'))
              }
            },
            trigger: 'change'
          }
        ],
        password: [
          { required: true, message: '必填', trigger: 'change' },
          { min: 6, max: 12, message: '请填写6-12位密码', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          await this.$store.dispatch('user/ToLogin', this.form)
          this.$router.push('/')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background-image: url('~@/assets/common/login.jpg');
  display: flex;
  justify-content: center;
  align-items: center;
  .box {
    width: 520px;
    height: 400px;
    // background-color: #fff;
    .img {
      text-align: center;
      padding-bottom: 30px;
    }
    .btn {
      width: 100%;
      padding: 20px;
      background-color: #407ffe;
      border: none;
    }
  }
}
</style>
